package com.dsp.soundscape.server;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.WordUtils;

import com.dsp.soundscape.model.AlbumInfo;

public class SoundscapeDataset {
	private Map <String, AlbumInfo> albums = new HashMap<String, AlbumInfo>();
	private Map <String, List<String>> albumsByArtist = new HashMap<String, List<String>>();;
	private Map <String, List<String>> albumsByYear = new HashMap<String, List<String>>();;
	
	private String latestYear;
	
	public void add(AlbumInfo album) {
		if (album.getYear() != null && album.getArtist() != null) {
			albums.put(album.getId(), album);
			
			List<String> artist = null;
			if (albumsByArtist.containsKey(album.getArtistRest())) {
				artist = albumsByArtist.get(album.getArtistRest());
			} else {
				artist = new ArrayList<String>();
				albumsByArtist.put(album.getArtistRest(), artist);
			}
			artist.add(album.getId());
			
			List<String> year = null;
			if (albumsByYear.containsKey(album.getYear())) {
				year = albumsByYear.get(album.getYear());
			} else {
				year = new ArrayList<String>();
				albumsByYear.put(album.getYear(), year);
			}
			year.add(album.getId());
		}

		Calendar today = Calendar.getInstance();
		int year = today.get(Calendar.YEAR);
		while (albumsByYear.size() > 0) {
			if (albumsByYear.containsKey(String.valueOf(year))) {
				latestYear = (String.valueOf(year));
				break;
			} else {
				year--;
			}
		}
	}
	
	public int getAlbumCount() {
		return albums.size();
	}

	public int getArtistCount() {
		return albumsByArtist.size();
	}
	
	public List<String> getYears() {
		List <String> years = new ArrayList<String>();
		years.addAll(albumsByYear.keySet());
		return years;
	}
	
	public List<String> getArtists() {
		List <String> artists = new ArrayList<String>();
		for (String artist : albumsByArtist.keySet()) {
			artists.add(WordUtils.capitalizeFully(artist, new char[] {' ', '-', '.'}));
		}
		return artists;
	}
	
	public List<AlbumInfo> getAlbumsByArtist(String artist) {
		List<AlbumInfo> currentView = new ArrayList <AlbumInfo>();
		for (String id : albumsByArtist.get(artist.toLowerCase())) {
			currentView.add(albums.get(id));
		}
		
		Collections.sort(currentView);
		return currentView;
	}
	
	public List<AlbumInfo> getAlbumsByYear(String year) {
		List<AlbumInfo> currentView = new ArrayList <AlbumInfo>();
		for (String id : albumsByYear.get(year)) {
			currentView.add(albums.get(id));
		}
		
		Collections.sort(currentView);
		return currentView;
	}
	
	public String getLatestYear() {
		return latestYear;
	}
	
	public AlbumInfo getAlbumInfo(String artist, String year, String album) {
		AlbumInfo info = albums.get(artist + "/" + year + "/" + album);
		return info;
	}
	
	public List<AlbumInfo> randomize() {
		Random random = new Random(System.currentTimeMillis());
		ArrayList <AlbumInfo> albumList = new ArrayList<AlbumInfo>();

		List <String> selected = new ArrayList<String>();
		List <String> artists = new ArrayList<String>();
		
		while (albumList.size() < 24) {
			int r1 = random.nextInt(getArtists().size());
			String artist = artists.get(r1);
			if (!selected.contains(artist.toLowerCase())) {
				List <String> albumKeys = albumsByArtist.get(artist.toLowerCase());
				int r2 = random.nextInt(albumKeys.size());
				albumList.add(albums.get(albumKeys.get(r2)));
				selected.add(artist.toLowerCase());
			}
		}
		
		Collections.sort(albumList);
		return albumList;
	}
}
